package com.xinongbest.mall.controller;

import com.xinongbest.mall.common.ApiRestResponse;
import com.xinongbest.mall.filter.UserFilter;
import com.xinongbest.mall.model.dto.CartDto;
import com.xinongbest.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @GetMapping("/list")
    public ApiRestResponse list() {
        List<CartDto> list = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(list);
    }

    @PostMapping("/add")
    public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer number) {
        Integer id = UserFilter.currentUser.getId();
        List<CartDto> cartDtoList = cartService.addCart(id, productId, number);
        return ApiRestResponse.success(cartDtoList);
    }

    @PostMapping("/update")
    public ApiRestResponse update(@RequestParam Integer productId, @RequestParam Integer number) {
        List<CartDto> update = cartService.update(UserFilter.currentUser.getId(), productId, number);
        return ApiRestResponse.success(update);
    }

    @PostMapping("/delete")
    public ApiRestResponse delete(Integer productId) {
        List<CartDto> delete = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(delete);
    }

    @PostMapping("/select")
    public ApiRestResponse select(@RequestParam Integer productId, @RequestParam Integer selected) {
        cartService.selectOrNot(UserFilter.currentUser.getId(), productId, selected);
        return ApiRestResponse.success();
    }

    @PostMapping("/selectAll")
    public ApiRestResponse selectAll(@RequestParam Integer selected) {
        cartService.selectAllOrNot(UserFilter.currentUser.getId(), selected);
        return ApiRestResponse.success();
    }
}
